<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\AdminUser;

class AuthController extends Controller
{
    public function toLogin()
    {
        return view('login');
    }

    public function login(Request $request)
    {

        $username = $request->user_name;
        $password = $request->password;
        $adminUser = AdminUser::where('account', $username)->first();
        if (!$adminUser) {
            $data = [
                'status' => 2,
                'msg'    => '用户不存在'
            ];
            return json_encode($data);
        }
        // if (Hash::check($password, $adminUser->password))
        if (md5($password) == $adminUser->md5pwd) {
            $request->session()->put('admin', $adminUser);
            $data = [
                'status' => 0,
                'msg'    => '登录成功',
                'url'    => route('promotion.create')
            ];
        } else {
            $data = [
                'status' => 3,
                'msg'    => '密码错误'
            ];
        }
        return json_encode($data);
    }

    public function logout(Request $request)
    {
        $request->session()->forget('admin');
        return redirect()->route('admin.toLogin');
    }
}
